package com.example.famhealing.schedule;

import java.util.Calendar;

import com.example.famhealing.R;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class DiaryFragment extends Fragment {

	EditText dayText;
	
	ImageButton setDate;
	
	RelativeLayout rl1, rl3, rl4, rl5, rl6, rl7, rl8;
	
	int curYear;
	int curMonth;
	int curDay;
	
	int year;
	int month;
	int day;
	
	String sDay;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.fragment_diary, container, false);
		
        dayText = (EditText) v.findViewById(R.id.dayText);
        
        Calendar cal= Calendar.getInstance ();
        
        cal.set(Calendar.YEAR, ScheduleActivity.selectedYear);
        cal.set(Calendar.MONTH, ScheduleActivity.selectedMonth-1);
        cal.set(Calendar.DATE, ScheduleActivity.selectedDay);
        
        sDay = getDay(cal);
        
        setDayText(ScheduleActivity.selectedYear, ScheduleActivity.selectedMonth, ScheduleActivity.selectedDay, sDay);
        
        setDate = (ImageButton) v.findViewById(R.id.setDate);
        setDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*
				new DatePickerDialog(getActivity(), dateSetListener, ScheduleActivity.selectedYear, 
						ScheduleActivity.selectedMonth-1, ScheduleActivity.selectedDay).show();
						*/
				
				//날짜 하나만 선택 가능하도록
		        CalendarMonthAdapter.multiSelect = false;
		        
		        //진료일정 추가할 날짜 선택위한 팝업창(달력) 띄우기
				Intent intent = new Intent(getActivity(), CalendarPopup.class);
				startActivity(intent);
			}
        	
        });
		
		rl1 = (RelativeLayout)v.findViewById(R.id.condition);
		rl3 = (RelativeLayout)v.findViewById(R.id.sideeffects);
		rl4 = (RelativeLayout)v.findViewById(R.id.weight_temper);
		rl5 = (RelativeLayout)v.findViewById(R.id.rate_pressure);
		rl7 = (RelativeLayout)v.findViewById(R.id.memo);
		
		rl1.setOnClickListener(onBtnClickListener);
		rl3.setOnClickListener(onBtnClickListener);
		rl4.setOnClickListener(onBtnClickListener);
		rl5.setOnClickListener(onBtnClickListener);
		rl7.setOnClickListener(onBtnClickListener);
		
		return v;
	}
	
	/*
	private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
	       
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                  int dayOfMonth) {
             // TODO Auto-generated method stub
        	
        	ScheduleActivity.selectedYear = year;
        	ScheduleActivity.selectedMonth = monthOfYear+1;
        	ScheduleActivity.selectedDay = dayOfMonth;
        	
        	Calendar cal= Calendar.getInstance ();
            
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DATE, dayOfMonth);
            
            sDay = getDay(cal);
        	
        	setDayText(year, monthOfYear+1, dayOfMonth, sDay);
        	
        }
	};
	*/
	
	private String getDay(Calendar calendar) {
		
		switch (calendar.get(Calendar.DAY_OF_WEEK)){
        case 1:
            sDay = "일";
            break;
        case 2:
        	sDay = "월";
            break;
        case 3:
        	sDay = "화";
            break;
        case 4:
        	sDay = "수";
            break;
        case 5:
        	sDay = "목";
            break;
        case 6:
        	sDay = "금";
            break;
        case 7:
        	sDay = "토";
            break;
        }
		return sDay;
	}
	
	private void setDayText(int year, int month, int date, String day) {
		
		if(month < 10) {
    		if(date < 10) {
    			dayText.setText(year + ".0" + month + ".0" + date + "(" + day + ")");
    		} else {
    			dayText.setText(year + ".0" + month + "." + date + "(" + day + ")");
    		}
    	} else {
    		if(date < 10) {
    			dayText.setText(year + "." + month + ".0" + date + "(" + sDay + ")");
    		} else {
    			dayText.setText(year + "." + month + "." + date + "(" + sDay + ")");
    		}
    	}
		
	}
	
	private OnClickListener onBtnClickListener = new OnClickListener()
	 {
		 @Override
	     public void onClick(View v)
		 {
			 switch(v.getId()) {
			 	case R.id.condition :
			 		Intent intent1 = new Intent(getActivity(), SetCondition.class);
			 		startActivity(intent1);
			 		break;
			 	case R.id.sideeffects :
			 		Intent intent3 = new Intent(getActivity(), SetSideEffects.class);
			 		startActivity(intent3);
			 		break;
			 	case R.id.weight_temper :
			 		Intent intent4 = new Intent(getActivity(), SetWeightTemper.class);
			 		startActivity(intent4);
			 		break;
			 	case R.id.rate_pressure :
			 		Intent intent5 = new Intent(getActivity(), SetRatePressure.class);
			 		startActivity(intent5);
			 		break;
			 	case R.id.memo :
			 		Intent intent7 = new Intent(getActivity(), SetMemo.class);
			 		startActivity(intent7);
			 		break;
			 }
	     }
	 };
}